<Project Sdk="Microsoft.DotNet.Helix.Sdk" DefaultTargets="Test">

  <!--
    This is useful for local testing to print the produced Helix items.
    To use this when you are changing how items are produced, uncomment the target
    and replace the Project item at the top of the file with this:

      <Project DefaultTargets="printItems">

    Once you've done that you can run this to see the results:

      dotnet msbuild .\superpmi-collect.proj /v:n

    ===========

  <PropertyGroup>
    <CorrelationPayloadDirectory>c:\bugs\spmicollect14\payload\correlation</CorrelationPayloadDirectory>
    <WorkItemDirectory>c:\bugs\spmicollect14\payload\workitem</WorkItemDirectory>
    <HelixTargetQueues>Some_Queue</HelixTargetQueues>
    <InputArtifacts>c:\bugs\spmicollect14\payload\workitem\collectAssembliesDirectory\libraries</InputArtifacts>
    <Architecture>x64</Architecture>
    <BuildConfig>Checked</BuildConfig>
    <MchFileTag>windows.x64.checked</MchFileTag>
    <CollectionName>libraries</CollectionName>
    <CollectionType>crossgen2</CollectionType>
    <AGENT_OS>Windows_NT</AGENT_OS>
  </PropertyGroup>
  <Target Name="printItems">
    <Message Text="@(HelixWorkItem -> 'name: %(HelixWorkItem.Identity)
    dir: %(HelixWorkItem.PayloadDirectory)
    pre: %(HelixWorkItem.PreCommands)
    command: %(HelixWorkItem.Command)
    post: %(HelixWorkItem.PostCommands)
    timeout: %(HelixWorkItem.Timeout)  '"/>
  </Target>

  -->

  <PropertyGroup Condition="'$(AGENT_OS)' == 'Windows_NT'">
    <FileSeparatorChar>\</FileSeparatorChar>
  </PropertyGroup>
  <PropertyGroup Condition="'$(AGENT_OS)' != 'Windows_NT'">
    <FileSeparatorChar>/</FileSeparatorChar>
  </PropertyGroup>

  <!-- The directory structure of collectAssembliesPayload is
       source\workitem\collectAssembliesDirectory\<machine_specific_load_0>\binaries
       source\workitem\collectAssembliesDirectory\<machine_specific_load_1>\binaries
       ...
       source\workitem\collectAssembliesDirectory\<machine_specific_load_N>\binaries

       AssembliesPayload          - Path that will be sent to helix machine to run collection on
       AssembliesDirectoryOnHelix - Path on helix machine itself where superpmi.py will discover the sent assemblies.
  -->

  <!-- OS-specific settings for all collections -->

  <PropertyGroup Condition="'$(AGENT_OS)' == 'Windows_NT' ">
    <Python>%HELIX_PYTHONPATH%</Python>
    <SuperPMIDirectory>%HELIX_CORRELATION_PAYLOAD%\superpmi</SuperPMIDirectory>
    <OutputMchPath>%HELIX_WORKITEM_UPLOAD_ROOT%</OutputMchPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(AGENT_OS)' != 'Windows_NT'">
    <Python>$HELIX_PYTHONPATH</Python>
    <SuperPMIDirectory>$HELIX_CORRELATION_PAYLOAD/superpmi</SuperPMIDirectory>
    <OutputMchPath>$HELIX_WORKITEM_UPLOAD_ROOT</OutputMchPath>
  </PropertyGroup>

  <!-- OS-specific settings for non-benchmarks collections -->

  <PropertyGroup Condition=" '$(CollectionName)' != 'benchmarks' and '$(CollectionName)' != 'realworld' and '$(AGENT_OS)' == 'Windows_NT' ">
    <AssembliesDirectoryOnHelix>%HELIX_WORKITEM_PAYLOAD%\binaries</AssembliesDirectoryOnHelix>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionName)' != 'benchmarks' and '$(CollectionName)' != 'realworld' and '$(AGENT_OS)' != 'Windows_NT' ">
    <AssembliesDirectoryOnHelix>$HELIX_WORKITEM_PAYLOAD/binaries</AssembliesDirectoryOnHelix>
  </PropertyGroup>

  <!-- OS-specific settings for benchmarks collections -->

  <PropertyGroup Condition=" ('$(CollectionName)' == 'benchmarks' or '$(CollectionName)' == 'realworld') and '$(AGENT_OS)' == 'Windows_NT' ">
    <PerformanceDirectory>%HELIX_WORKITEM_PAYLOAD%\performance</PerformanceDirectory>
  </PropertyGroup>
  <PropertyGroup Condition=" ('$(CollectionName)' == 'benchmarks' or '$(CollectionName)' == 'realworld') and '$(AGENT_OS)' != 'Windows_NT' ">
    <PerformanceDirectory>$HELIX_WORKITEM_PAYLOAD/performance</PerformanceDirectory>
  </PropertyGroup>

  <!-- Common non-OS-specific settings -->

  <PropertyGroup>
    <AssembliesPayload>$(WorkItemDirectory)$(FileSeparatorChar)collectAssembliesDirectory$(FileSeparatorChar)$(CollectionName)</AssembliesPayload>
  </PropertyGroup>

  <!-- Settings for PMI collections -->

  <ItemGroup>
    <!-- Relative directories in the $(SuperPMIDirectory) that should be added to the PMIPATH variable
         (via `superpmi.py -pmi_path` argument) for PMI collections.
    -->
    <PmiPathDirectories Include="R2RDump" />
    <PmiPathDirectories Include="R2RTest" />
    <PmiPathDirectories Include="crossgen2" />
    <PmiPathDirectories Include="xunit" />
  </ItemGroup>

  <PropertyGroup Condition=" '$(CollectionType)' == 'pmi' and '$(AGENT_OS)' == 'Windows_NT' ">
    <PmiArguments>-pmi_location $(SuperPMIDirectory)\pmi.dll -pmi_path @(PmiPathDirectories->'$(SuperPMIDirectory)\%(Identity)', ' ')</PmiArguments>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionType)' == 'pmi' and '$(AGENT_OS)' != 'Windows_NT' ">
    <PmiArguments>-pmi_location $(SuperPMIDirectory)/pmi.dll -pmi_path @(PmiPathDirectories->'$(SuperPMIDirectory)/%(Identity)', ' ')</PmiArguments>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionType)' != 'pmi' ">
    <PmiArguments></PmiArguments>
  </PropertyGroup>

  <!-- Settings for Run collections -->

  <PropertyGroup Condition=" '$(CollectionType)' == 'run_tiered' ">
    <RunArguments>--tiered_compilation</RunArguments>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionType)' == 'run_pgo' ">
    <RunArguments>--tiered_pgo</RunArguments>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionType)' != 'run_tiered' and '$(CollectionType)' != 'run_pgo' ">
    <RunArguments></RunArguments>
  </PropertyGroup>

  <!-- Settings for input kind -->

  <PropertyGroup Condition=" '$(CollectionType)' == 'nativeaot' ">
    <InputKind>-ilc_rsps</InputKind>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(CollectionType)' != 'nativeaot' ">
    <InputKind>-assemblies</InputKind>
  </PropertyGroup>

  <!-- Define the Helix work item command and timeout for each collection type -->

  <PropertyGroup Condition="'$(CollectionName)' != 'benchmarks' and '$(CollectionName)' != 'realworld'">
    <WorkItemCommand>$(Python) $(SuperPMIDirectory)$(FileSeparatorChar)superpmi.py collect --clean -log_level DEBUG --$(CollectionType) $(PmiArguments) $(InputKind) $(AssembliesDirectoryOnHelix) -arch $(Architecture) -build_type $(BuildConfig) -core_root $(SuperPMIDirectory)</WorkItemCommand>
    <WorkItemTimeout>2:00</WorkItemTimeout>
  </PropertyGroup>

  <PropertyGroup Condition="'$(CollectionName)' == 'benchmarks' or '$(CollectionName)' == 'realworld'">
    <WorkItemCommand>$(Python) $(SuperPMIDirectory)$(FileSeparatorChar)superpmi_benchmarks.py -performance_directory $(PerformanceDirectory) -superpmi_directory $(SuperPMIDirectory) -core_root $(SuperPMIDirectory) -arch $(Architecture) $(RunArguments)</WorkItemCommand>
    <WorkItemTimeout>3:00</WorkItemTimeout>
  </PropertyGroup>

  <PropertyGroup>
    <EnableAzurePipelinesReporter>false</EnableAzurePipelinesReporter>
    <EnableXUnitReporter>false</EnableXUnitReporter>
  </PropertyGroup>

  <ItemGroup Condition=" '$(AGENT_OS)' == 'Windows_NT' ">
    <HelixPreCommand Include="taskkill.exe /f /im corerun.exe"/>
    <HelixPostCommand Include="taskkill.exe /f /im corerun.exe"/>
  </ItemGroup>

  <!-- We're currently not using .dotnet on the Helix agents -->
  <!--
  <ItemGroup Condition=" '$(AGENT_OS)' == 'Windows_NT' ">
    <HelixPreCommand Include="set PATH=%HELIX_CORRELATION_PAYLOAD%\.dotnet%3B%PATH%" Condition=" '$(CollectionType)' == 'crossgen2' "/>
  </ItemGroup>
  <ItemGroup Condition=" '$(AGENT_OS)' != 'Windows_NT' ">
    <HelixPreCommand Include="export PATH=$HELIX_CORRELATION_PAYLOAD/.dotnet:$PATH" Condition=" '$(CollectionType)' == 'crossgen2' "/>
  </ItemGroup>
  -->

  <PropertyGroup>
    <HelixPreCommands>@(HelixPreCommand)</HelixPreCommands>
    <HelixPostCommands>@(HelixPostCommand)</HelixPostCommands>
  </PropertyGroup>

  <ItemGroup>
    <HelixCorrelationPayload Include="$(CorrelationPayloadDirectory)">
      <PayloadDirectory>%(Identity)</PayloadDirectory>
    </HelixCorrelationPayload>
  </ItemGroup>

  <!-- For non-benchmark collections, we create one Helix work item for each sub-directory
       in the `InputArtifacts` directory.
  -->
  <ItemGroup Condition="'$(CollectionName)' != 'benchmarks' and '$(CollectionName)' != 'realworld'">
    <PartitionDirectories Include="$([System.IO.Directory]::GetDirectories($(InputArtifacts)))"/>
    <Partition Include="@(PartitionDirectories -> '%(Filename)')" CollectAssemblies="%(Filename)" PartitionId="%(Filename)" />
  </ItemGroup>

  <!-- For benchmark collections, we create one Helix work item for each `BDN_Partition` item
       in the ItemGroup below.
  -->
  <PropertyGroup Condition="'$(CollectionName)' == 'benchmarks' and '$(AGENT_OS)' == 'Windows_NT'">
    <PartitionCount>30</PartitionCount>
    <BenchmarkPath>src\benchmarks\micro\MicroBenchmarks.csproj</BenchmarkPath>
    <BenchmarkBinary>MicroBenchmarks.dll</BenchmarkBinary>
  </PropertyGroup>
  <PropertyGroup Condition="'$(CollectionName)' == 'benchmarks' and '$(AGENT_OS)' != 'Windows_NT'">
    <PartitionCount>30</PartitionCount>
    <BenchmarkPath>src/benchmarks/micro/MicroBenchmarks.csproj</BenchmarkPath>
    <BenchmarkBinary>MicroBenchmarks.dll</BenchmarkBinary>
  </PropertyGroup>
  <ItemGroup Condition="'$(CollectionName)' == 'benchmarks'">
    <BDN_Partition Include="Partition0" Index="0" />
    <BDN_Partition Include="Partition1" Index="1" />
    <BDN_Partition Include="Partition2" Index="2" />
    <BDN_Partition Include="Partition3" Index="3" />
    <BDN_Partition Include="Partition4" Index="4" />
    <BDN_Partition Include="Partition5" Index="5" />
    <BDN_Partition Include="Partition6" Index="6" />
    <BDN_Partition Include="Partition7" Index="7" />
    <BDN_Partition Include="Partition8" Index="8" />
    <BDN_Partition Include="Partition9" Index="9" />
    <BDN_Partition Include="Partition10" Index="10" />
    <BDN_Partition Include="Partition11" Index="11" />
    <BDN_Partition Include="Partition12" Index="12" />
    <BDN_Partition Include="Partition13" Index="13" />
    <BDN_Partition Include="Partition14" Index="14" />
    <BDN_Partition Include="Partition15" Index="15" />
    <BDN_Partition Include="Partition16" Index="16" />
    <BDN_Partition Include="Partition17" Index="17" />
    <BDN_Partition Include="Partition18" Index="18" />
    <BDN_Partition Include="Partition19" Index="19" />
    <BDN_Partition Include="Partition20" Index="20" />
    <BDN_Partition Include="Partition21" Index="21" />
    <BDN_Partition Include="Partition22" Index="22" />
    <BDN_Partition Include="Partition23" Index="23" />
    <BDN_Partition Include="Partition24" Index="24" />
    <BDN_Partition Include="Partition25" Index="25" />
    <BDN_Partition Include="Partition26" Index="26" />
    <BDN_Partition Include="Partition27" Index="27" />
    <BDN_Partition Include="Partition28" Index="28" />
    <BDN_Partition Include="Partition29" Index="29" />
  </ItemGroup>
  <ItemGroup Condition="'$(CollectionName)' == 'realworld' and '$(AGENT_OS)' == 'Windows_NT'">
    <BDN_Partition Include="Partition0" BenchmarkPath="src\benchmarks\real-world\bepuphysics2\DemoBenchmarks.csproj" BenchmarkBinary="DemoBenchmarks.dll" />
    <BDN_Partition Include="Partition1" BenchmarkPath="src\benchmarks\real-world\FSharp\FSharp.fsproj" BenchmarkBinary="FSharp.dll" />
    <BDN_Partition Include="Partition2" BenchmarkPath="src\benchmarks\real-world\ILLink\ILLinkBenchmarks.csproj" BenchmarkBinary="ILLinkBenchmarks.dll" />
    <BDN_Partition Include="Partition3" BenchmarkPath="src\benchmarks\real-world\ImageSharp\ImageSharp.Benchmarks.csproj" BenchmarkBinary="ImageSharp.Benchmarks.dll" />
    <BDN_Partition Include="Partition4" BenchmarkPath="src\benchmarks\real-world\Microsoft.ML.Benchmarks\Microsoft.ML.Benchmarks.csproj" BenchmarkBinary="Microsoft.ML.Benchmarks.dll" />
    <BDN_Partition Include="Partition5" BenchmarkPath="src\benchmarks\real-world\Roslyn\CompilerBenchmarks.csproj" BenchmarkBinary="CompilerBenchmarks.dll" />
    <BDN_Partition Include="Partition6" BenchmarkPath="src\benchmarks\real-world\PowerShell.Benchmarks\PowerShell.Benchmarks.csproj" BenchmarkBinary="PowerShell.Benchmarks.dll" />
  </ItemGroup>  
  <ItemGroup Condition="'$(CollectionName)' == 'realworld' and '$(AGENT_OS)' != 'Windows_NT'">
    <BDN_Partition Include="Partition0" BenchmarkPath="src/benchmarks/real-world/bepuphysics2/DemoBenchmarks.csproj" BenchmarkBinary="DemoBenchmarks.dll" />
    <BDN_Partition Include="Partition1" BenchmarkPath="src/benchmarks/real-world/FSharp/FSharp.fsproj" BenchmarkBinary="FSharp.dll" />
    <BDN_Partition Include="Partition2" BenchmarkPath="src/benchmarks/real-world/ILLink/ILLinkBenchmarks.csproj" BenchmarkBinary="ILLinkBenchmarks.dll" />
    <BDN_Partition Include="Partition3" BenchmarkPath="src/benchmarks/real-world/ImageSharp/ImageSharp.Benchmarks.csproj" BenchmarkBinary="ImageSharp.Benchmarks.dll" />
    <BDN_Partition Include="Partition4" BenchmarkPath="src/benchmarks/real-world/Microsoft.ML.Benchmarks/Microsoft.ML.Benchmarks.csproj" BenchmarkBinary="Microsoft.ML.Benchmarks.dll" />
    <BDN_Partition Include="Partition5" BenchmarkPath="src/benchmarks/real-world/Roslyn/CompilerBenchmarks.csproj" BenchmarkBinary="CompilerBenchmarks.dll" />
    <BDN_Partition Include="Partition6" BenchmarkPath="src/benchmarks/real-world/PowerShell.Benchmarks/PowerShell.Benchmarks.csproj" BenchmarkBinary="PowerShell.Benchmarks.dll" />
  </ItemGroup>  

  <ItemGroup Condition="'$(CollectionName)' != 'benchmarks' and '$(CollectionName)' != 'realworld'">
    <HelixWorkItem Include="@(Partition)">
      <OutputFileName>$(CollectionName).$(CollectionType).%(HelixWorkItem.PartitionId).$(MchFileTag)</OutputFileName>
      <PayloadDirectory>$(AssembliesPayload)$(FileSeparatorChar)%(HelixWorkItem.CollectAssemblies)</PayloadDirectory>
      <Command>$(WorkItemCommand) -output_mch_path $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).mch -log_file $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).log</Command>
      <Timeout>$(WorkItemTimeout)</Timeout>
      <DownloadFilesFromResults>%(OutputFileName).mch;%(OutputFileName).mch.mct;%(OutputFileName).log</DownloadFilesFromResults>
    </HelixWorkItem>
  </ItemGroup>

  <ItemGroup Condition="'$(CollectionName)' == 'benchmarks'">
    <HelixWorkItem Include="@(BDN_Partition)">
      <OutputFileName>$(CollectionName).$(CollectionType).%(HelixWorkItem.Index).$(MchFileTag)</OutputFileName>
      <PayloadDirectory>$(WorkItemDirectory)</PayloadDirectory>
      <Command>$(WorkItemCommand) -partition_count $(PartitionCount) -partition_index %(HelixWorkItem.Index) -benchmark_path $(BenchmarkPath) -benchmark_binary $(BenchmarkBinary) -output_mch_path $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).mch -log_file $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).log</Command>
      <Timeout>$(WorkItemTimeout)</Timeout>
      <DownloadFilesFromResults>%(OutputFileName).mch;%(OutputFileName).mch.mct;%(OutputFileName).log</DownloadFilesFromResults>
    </HelixWorkItem>
  </ItemGroup>

  <ItemGroup Condition="'$(CollectionName)' == 'realworld'">
    <HelixWorkItem Include="@(BDN_Partition)">
      <OutputFileName>$(CollectionName).$(CollectionType).%(HelixWorkItem.Index).$(MchFileTag)</OutputFileName>
      <PayloadDirectory>$(WorkItemDirectory)</PayloadDirectory>
      <Command>$(WorkItemCommand) -partition_count 1 -partition_index 0 -benchmark_path %(HelixWorkItem.BenchmarkPath) -benchmark_binary %(HelixWorkItem.BenchmarkBinary) -output_mch_path $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).mch -log_file $(OutputMchPath)$(FileSeparatorChar)%(OutputFileName).log</Command>
      <Timeout>$(WorkItemTimeout)</Timeout>
      <DownloadFilesFromResults>%(OutputFileName).mch;%(OutputFileName).mch.mct;%(OutputFileName).log</DownloadFilesFromResults>
    </HelixWorkItem>
  </ItemGroup>  
</Project>
